/*
 * ©2009-2018 南京擎盾信息科技有限公司 All rights reserved.
 */
package com.wuhao.code.check.inspection.visitor

import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.lang.Language
import com.intellij.lang.java.JavaLanguage
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.intellij.psi.impl.source.PsiClassReferenceType


/**
 * Java代码格式检查访问器
 * Created by 吴昊 on 18-4-26.
 * @author 吴昊
 * @since 1.1
 */
class JavaMapperMethodVisitor(private val holder: ProblemsHolder) :
    JavaElementVisitor(), BaseCodeFormatVisitor {

  override fun support(language: Language): Boolean {
    return language == JavaLanguage.INSTANCE
  }

  override fun visitMethod(method: PsiMethod) {
    val modelType = getXmllessMapperModelType(method)
    if (modelType != null && modelType is PsiClass) {
      holder.registerProblem(
          method,
          "[XMLLess]：生成脚本并在对话框中打卡",
          ProblemHighlightType.INFORMATION,
          XmllessGeneratorInDialog()
      )
      holder.registerProblem(
          method,
          "[XMLLess]：生成脚本并在编辑器中打卡",
          ProblemHighlightType.INFORMATION,
          XmllessGeneratorInEditor()
      )
    }

    return super.visitMethod(method)
  }


}


/**
 * @param method
 * @return
 */
fun getXmllessMapperModelType(method: PsiMethod): PsiElement? {
  val clazz = method.containingClass
  clazz?.superTypes?.forEach {
    if (it is PsiClassReferenceType) {
      val ref = it.reference
      if (ref.typeParameters.size == 1 && ref.referenceName == "XmlLessMapper") {
        val type = ref.typeParameters[0]
        if (type is PsiClassReferenceType) {
          return type.reference.resolve()
        }
      }
    }
  }
  return null
}
